<?php

namespace App\AgentAdmin\Controllers;

use App\Models\AgentAccount;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Alert;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Column;

class AgentAccountController extends AdminController
{
    
    public function edit($id,Content $content)
    {
        $alert = Alert::make( "修改右侧收款信息需填写【授权码】，请联系客服获取，每个授权码仅可使用一次",'提示');
        $account = AgentAccount::where('agent_id',(Admin::user()->id))->first();
        if($account){
            $account = $account->toArray();
        }
        return $content
            ->header('资金账户设置')
            ->body(
            function (Row $row) use($alert,$account){
                $row->column(6, function (Column $column)use($alert,$account)  {
                    $column->row($alert->warning());
                    $column->row($this->form2()->edit($account['id']));
                });
                $row->column(6, function (Column $column) use($account) {
                    $column->row($this->form()->edit($account['id']));
                });
            });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AgentAccount(), function (Form $form) {
            $form->text('bank_receiver');
            $form->text('bank_name');
            $form->text('bank_number');
            $form->text('ali_account');
            $form->image('ali_qr')->autoUpload()->retainable()->removable(false)->autoSave(false);
            $form->text('wx_receiver');
            $form->image('wx_qr')->autoUpload()->retainable()->removable(false)->autoSave(false);
            // $form->hidden('edit_code');
            $form->text('confirm_edit_code','授权码')->required()->help('请联系客服获取，每个授权码仅可使用一次');

            $form->disableHeader();
            $form->footer(function ($footer) {

                // 去掉`查看`checkbox
                $footer->disableViewCheck();
            
                // 去掉`继续编辑`checkbox
                $footer->disableEditingCheck();
            
                // 去掉`继续创建`checkbox
                $footer->disableCreatingCheck();
            });

            $form->saving(function(Form $form){
                if($form->confirm_edit_code!=$form->model()->edit_code){
                    return $form->response()->alert(true)->error('授权码不正确');
                }
                $form->deleteInput('confirm_edit_code');
            });
            $form->saved(function(Form $form){
                $form->model()->update(['edit_code' => random(6)]);
                return $form->response()->success('保存成功')->redirect("agent-accounts/1/edit");
            });
        });
    }


    protected function form2()
    {
        return Form::make(new AgentAccount(), function (Form $form) {
            $form->display('balance')->help('账户余额');
            $form->display('profits')->help('总收益');
            $form->display('withdrawed')->help('已提现金额');


            $form->tools(function (Form\Tools $tools) {
                // 去掉跳转列表按钮
                $tools->disableList();
                // 去掉跳转详情页按钮
                $tools->disableView();
                // 去掉删除按钮
                $tools->disableDelete();
            
                // 添加一个按钮, 参数可以是字符串, 匿名函数, 或者实现了Renderable或Htmlable接口的对象实例
                $prefix = config('agent-admin.route.prefix');
                $url = "/{$prefix}/withdraws/create";
                $tools->append("<a href={$url} class='btn btn-sm btn-primary text-white'>申请提现</a>");
            });
            // $form->disableHeader();
            $form->disableFooter();

            
        });
    }
}
